/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Divorce.java
 *
 * Created on 6 oct. 2011, 11:41:45
 */
package Vue;

import Bdd.DivorceRepository;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Babs / Dave
 */
public class Divorce extends javax.swing.JDialog {
    
    private static final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    /** Creates new form Divorce */
    public Divorce(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        // Insertion des numeros pour la date
        int yearNow = 2011;
        for (int i = 1; i <= 31; i++) {
            comboBoxDay1.addItem(String.valueOf(i));
        }
        for (int i = 0; i < 12; i++) {
            comboBoxMonth1.addItem(months[i]);
        }
        for (int i = yearNow; i >= 1800; i--) {
            comboBoxYear1.addItem(String.valueOf(i));
        }
        
        vipRepo = new Bdd.VipRepository();
        ArrayList<Metier.Vip> allMaries = vipRepo.loadAllMaries();
        // Insertion des données dans les comboBox à l'aide de modeles
        comboBoxMenModel = new DefaultComboBoxModel(allMaries.toArray());
        cbMaries.setModel(comboBoxMenModel);
        labelConjoint.setText(vipRepo.getConjoint((Metier.Vip) cbMaries.getSelectedItem()).toString());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titleWhen = new javax.swing.JLabel();
        comboBoxDay = new javax.swing.JComboBox();
        comboBoxMonth = new javax.swing.JComboBox();
        comboBoxYear = new javax.swing.JComboBox();
        Ltitle = new javax.swing.JLabel();
        BtnProceed = new javax.swing.JButton();
        cbMaries = new javax.swing.JComboBox();
        Lbetween = new javax.swing.JLabel();
        Land = new javax.swing.JLabel();
        labelConjoint = new javax.swing.JLabel();
        labelError = new javax.swing.JLabel();
        buttonCancel = new javax.swing.JButton();
        titleWhen1 = new javax.swing.JLabel();
        comboBoxDay1 = new javax.swing.JComboBox();
        comboBoxMonth1 = new javax.swing.JComboBox();
        comboBoxYear1 = new javax.swing.JComboBox();

        titleWhen.setText("When ?");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        Ltitle.setFont(new java.awt.Font("Aston-F1", 0, 24));
        Ltitle.setText("proceed in a divorce");

        BtnProceed.setText("Proceed");
        BtnProceed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnProceedActionPerformed(evt);
            }
        });

        cbMaries.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbMariesItemStateChanged(evt);
            }
        });

        Lbetween.setText("Between");

        Land.setText("and");

        labelConjoint.setText("jLabel1");

        labelError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelError.setText(" ");

        buttonCancel.setText("Cancel");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        titleWhen1.setText("When ?");

        comboBoxDay1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxDay1ActionPerformed(evt);
            }
        });

        comboBoxMonth1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxMonth1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(239, Short.MAX_VALUE)
                .addComponent(Ltitle)
                .addGap(45, 45, 45))
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Land)
                    .addComponent(Lbetween))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelConjoint, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                    .addComponent(cbMaries, 0, 265, Short.MAX_VALUE))
                .addGap(95, 95, 95))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(titleWhen1)
                .addGap(18, 18, 18)
                .addComponent(comboBoxDay1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboBoxMonth1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboBoxYear1, 0, 195, Short.MAX_VALUE)
                .addGap(28, 28, 28))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelError, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(366, Short.MAX_VALUE)
                .addComponent(buttonCancel)
                .addGap(27, 27, 27)
                .addComponent(BtnProceed)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(Ltitle)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbetween)
                    .addComponent(cbMaries, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Land)
                    .addComponent(labelConjoint))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleWhen1)
                    .addComponent(comboBoxDay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxYear1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxMonth1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(BtnProceed))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnProceedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnProceedActionPerformed
        labelError.setText("  ");

        // Validation de la date //
        if (!isValid(Integer.valueOf((String) comboBoxDay1.getSelectedItem()), (comboBoxMonth1.getSelectedIndex() + 1), Integer.valueOf((String) comboBoxYear1.getSelectedItem()))) {
            labelError.setText("Please enter a valid date... (" + (String) comboBoxDay1.getSelectedItem() + "-" + (comboBoxMonth1.getSelectedIndex() + 1) + "-" + (String) comboBoxYear1.getSelectedItem() + ")");
            return;
        }
        System.out.println("Création date : " + Integer.valueOf((String) comboBoxYear1.getSelectedItem()) + "-" + (comboBoxMonth1.getSelectedIndex() + 1) + "-" + Integer.valueOf((String) comboBoxDay1.getSelectedItem()));
        Date dateActu = new Date(Integer.valueOf((String) comboBoxYear1.getSelectedItem())-1900, (comboBoxMonth1.getSelectedIndex()), Integer.valueOf((String) comboBoxDay1.getSelectedItem()));
        Metier.Vip vipConjoint = vipRepo.getConjoint(((Metier.Vip)comboBoxMenModel.getSelectedItem()));
        
        System.out.println("Comparaison date "+((Metier.Vip)comboBoxMenModel.getSelectedItem()).getDate_naiss()+" et "+dateActu);
        System.out.println("Comparaison date "+vipConjoint.getDate_naiss()+" et "+dateActu);
        if ((((Metier.Vip) comboBoxMenModel.getSelectedItem()).getDate_naiss().compareTo(dateActu)) > 0 || (vipConjoint.getDate_naiss().compareTo(dateActu) > 0)) {
            labelError.setText("Date impossible, look at birth dates...");
            return;
        }
        
        Metier.Divorce divorceActu = new Metier.Divorce((Metier.Vip) comboBoxMenModel.getSelectedItem(), vipConjoint, dateActu);
        Bdd.DivorceRepository divorceRepo = new Bdd.DivorceRepository();
        int resultInsertion = divorceRepo.insertObject(divorceActu);
        switch (resultInsertion) {
            case DivorceRepository.INSERT_OK:
                this.dispose();
                return;
            case DivorceRepository.INSERT_ERROR_EXIST:
                labelError.setText("This aleady exist !");
                return;
            case DivorceRepository.INSERT_ERROR_VIP:
                labelError.setText("Oups, Vip selected are deleted...");
                return;
            case DivorceRepository.INSERT_ERROR_NET:
                labelError.setText("Connection error...");
                return;
            default:
                labelError.setText("Unknow error...");
                return;
        }
}//GEN-LAST:event_BtnProceedActionPerformed

    private void cbMariesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbMariesItemStateChanged
        labelConjoint.setText(vipRepo.getConjoint((Metier.Vip) cbMaries.getSelectedItem()).toString());
    }//GEN-LAST:event_cbMariesItemStateChanged

private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
    this.setVisible(false);
}//GEN-LAST:event_buttonCancelActionPerformed

private void comboBoxDay1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxDay1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_comboBoxDay1ActionPerformed

private void comboBoxMonth1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxMonth1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_comboBoxMonth1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnProceed;
    private javax.swing.JLabel Land;
    private javax.swing.JLabel Lbetween;
    private javax.swing.JLabel Ltitle;
    private javax.swing.JButton buttonCancel;
    private javax.swing.JComboBox cbMaries;
    private javax.swing.JComboBox comboBoxDay;
    private javax.swing.JComboBox comboBoxDay1;
    private javax.swing.JComboBox comboBoxMonth;
    private javax.swing.JComboBox comboBoxMonth1;
    private javax.swing.JComboBox comboBoxYear;
    private javax.swing.JComboBox comboBoxYear1;
    private javax.swing.JLabel labelConjoint;
    private javax.swing.JLabel labelError;
    private javax.swing.JLabel titleWhen;
    private javax.swing.JLabel titleWhen1;
    // End of variables declaration//GEN-END:variables
    private Bdd.VipRepository vipRepo;
    private DefaultComboBoxModel comboBoxMenModel;
    
    public boolean isValid(int jour, int mois, int annee) {
        Calendar c = Calendar.getInstance();
        c.setLenient(false);
        c.set(annee, mois, jour);
        try {
            c.getTime();
        } catch (IllegalArgumentException iAE) {
            return false;
        }
        return true;
    }
}
